<?php

/*
  Uploadify
  Copyright (c) 2012 Reactive Apps, Ronnie Garcia
  Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
 */

require ('../../classes/GalleryManager.class.php');

define (IMAGE_WIDTH, 1140);
define (THUMB_WIDTH, 260);

// Define a destination
$targetFolder = '/galleries'; // Relative to the root

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = '../..' . $targetFolder . '/' . $_POST['year'];
    if (!file_exists($targetPath)) {
        mkdir($targetPath);
    }
    $targetPath .= '/' . $_POST['month'];
    if (!file_exists($targetPath)) {
        mkdir($targetPath);
    }

    $targetPath .= '/' . $_POST['foldername'];

    if (!file_exists($targetPath)) {
        mkdir($targetPath);
    }

    $targetPathThumb = $targetPath . '/thumbnails';
    $targetPathPhoto = $targetPath . '/photos';

    if (!file_exists($targetPathPhoto)) {
        mkdir($targetPathPhoto);
    }

    if (!file_exists($targetPathThumb)) {
        mkdir($targetPathThumb);
    }
    
    $now = new DateTime('now');

    // Validate the file type
    $fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'JPG', 'JPEG', 'GIF', 'PNG'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    $file_name = $now->format('Ymd_His') . '.' . $fileParts['extension'];
    $targetFilePhoto = rtrim($targetPathPhoto, '/') . '/' . $file_name;
    $targetFileThumb = rtrim($targetPathThumb, '/') . '/' . $file_name;
    
    if (in_array($fileParts['extension'], $fileTypes)) {

        list($width, $height, $type) = getimagesize($tempFile);
        $ratio = $width / $height;
        if ($width > IMAGE_WIDTH) {
            

            $resized_image = imagecreatetruecolor(IMAGE_WIDTH, IMAGE_WIDTH / $ratio);

            switch ($type) {
                case IMAGETYPE_JPEG:
                    $tmp_image_file = imagecreatefromjpeg($tempFile);
                    break;
                case IMAGETYPE_GIF:
                    $tmp_image_file = imagecreatefromgif($tempFile);
                    break;
                case IMAGETYPE_PNG:
                    $tmp_image_file = imagecreatefrompng($tempFile);
                    break;
            }
            imagecopyresampled($resized_image, $tmp_image_file, 0, 0, 0, 0, IMAGE_WIDTH, IMAGE_WIDTH / $ratio, $width, $height);

            switch ($type) {
                case IMAGETYPE_JPEG:
                    imagejpeg($resized_image, $targetFilePhoto, 100);
                    break;
                case IMAGETYPE_GIF:
                    imagegif($resized_image, $targetFilePhoto);
                    break;
                case IMAGETYPE_PNG:
                    imagepng($resized_image, $targetFilePhoto, 100);
                    break;
            }

            imagedestroy($resized_image);
        } else {
            move_uploaded_file($tempFile, $targetFilePhoto);
        }
        
        $thumb_image = imagecreatetruecolor(THUMB_WIDTH, THUMB_WIDTH / $ratio);
        
        imagecopyresampled($thumb_image, $tmp_image_file, 0, 0, 0, 0, THUMB_WIDTH, THUMB_WIDTH / $ratio, $width, $height);

        switch ($type) {
            case IMAGETYPE_JPEG:
                imagejpeg($thumb_image, $targetFileThumb, 100);
                break;
            case IMAGETYPE_GIF:
                imagegif($thumb_image, $targetFileThumb);
                break;
            case IMAGETYPE_PNG:
                imagepng($thumb_image, $targetFileThumb, 100);
                break;
        }

        imagedestroy($thumb_image);

        $gm = new GalleryManager();

        $p = new Photo();
        $p->gallery_id = $_POST['gallery_id'];
        $p->url = $file_name;

        $gm->save_photo_for_gallery($p);
        
        echo json_encode(array('url' => 'http://www.salesianichioggia.it/galleries/' . $_POST['year'] . '/' . $_POST['month'] . '/' . $_POST['foldername'] . '/thumbnails/' . $file_name,
                   'id' => $p->id
                  ));
    } else {
        echo 'Invalid file type.';
    }
}
?>